package com.sc.nft.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.vo.EcologyPoolGetPoolNameByDaoIdVo;
import com.sc.nft.entity.vo.EcologyPoolHatchCollectionVo;
import com.sc.nft.entity.vo.UserCreatorInfoExcelVO;
import com.sc.nft.entity.vo.ecologypool.*;
import com.sc.nft.enums.EcologyPoolTypeEnum;
import com.sc.nft.service.EcologyPoolDaoRankConfService;
import com.sc.nft.service.EcologyPoolDaoRankService;
import com.sc.nft.service.EcologyPoolService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 刘国航
 * @since 2024/7/13
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ecologyPool")
@Api("生态池")
public class EcologyPoolController {

    private final EcologyPoolService ecologyPoolService;
    private final EcologyPoolDaoRankConfService ecologyPoolDaoRankConfService;
    private final EcologyPoolDaoRankService ecologyPoolDaoRankService;

    @ApiOperation(value = "获取生态池积分", notes = "获取生态池积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPoolData", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Map<EcologyPoolTypeEnum, EcologyPoolDataVO>> getPoolData(@RequestBody EcologyPoolDataRequest request) {
        return Result.success(ecologyPoolService.getPoolData(request.getTypeList(), null, null));
    }

    @ApiOperation(value = "获取生态熔炼积分", notes = "获取生态熔炼积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/sourceDayPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<EcologyPoolSourceDayPageVO> sourceDayPage(@RequestBody PageRequest request) {
        return PageResult.success(ecologyPoolService.sourceDayPage(request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "获取生态熔炼积分-积分明细", notes = "获取生态熔炼积分-积分明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/sourceDetailPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<EcologyPoolSourceDetailVO> sourceDetailPage(@RequestBody @Valid EcologyPoolSourceDetailRequest request) {
        return PageResult.success(ecologyPoolService.sourceDetailPage(request.getPageNo(), request.getPageSize(), request.getLinkDate()));
    }

    @ApiOperation(value = "查询积分明细", notes = "查询积分明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/sourcePage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<EcologyPoolSourceVO> sourcePage(@RequestBody @Valid EcologyPoolSourceRequest request) {
        return PageResult.success(ecologyPoolService.sourcePage(request.getPageNo(), request.getPageSize(), request.getUserTel(), request.getCollectionName(), request.getStatus(), request.getStartTime(), request.getEndTime()));
    }

    @ApiOperation(value = "生态流水", notes = "生态流水", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/recordPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<EcologyPoolRecordVO> recordPage(@RequestBody @Valid EcologyPoolRecordRequest request) {
        return PageResult.success(ecologyPoolService.recordPage(request.getPageNo(), request.getPageSize(), request.getPoolType(), request.getFlowType(), request.getStartTime(), request.getEndTime(), request.getDaoName()));
    }

    @ApiOperation(value = "官方生态池分配/扣除积分", notes = "官方生态池分配/扣除积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/officialAllocatePoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> officialAllocatePoints(@RequestBody @Valid EcologyPoolOfficialAllocatePointsRequest request) {
        return Result.success(ecologyPoolService.officialAllocatePoints(request.getOperationType(), request.getExcellentBrandCount(), request.getServiceCenterCount(), request.getCommunityNetworkCount(), request.getLinkType(), request.getRemark()));
    }

    @ApiOperation(value = "优秀品牌池分配/扣除流水", notes = "优秀品牌池分配/扣除流水", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/excellentBrandAllocatePoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> excellentBrandAllocatePoints(@RequestBody @Valid EcologyPoolExcellentBrandAllocatePointsRequest request) {
        return Result.success(ecologyPoolService.excellentBrandAllocatePoints(request.getOperationType(), request.getDaoId(), request.getLinkCount(), request.getLinkType(), request.getRemark()));
    }

    @ApiOperation(value = "按品牌查询品牌生态池积分", notes = "按品牌查询品牌生态池积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findPointsByDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<EcologyPoolFindPointsByDaoVO> findPointsByDao(@RequestBody @Valid EcologyPoolFindPointsByDaoRequest request) {
        return Result.success(ecologyPoolService.findPointsByDao(request.getDaoId(), request.getLinkType()));
    }

    @ApiOperation(value = "查询已开启入榜的品牌", notes = "查询已开启入榜的品牌", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getEnableJoinRankDaos", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<EcologyPoolGetEnableJoinRankDaoPageVo>> getEnableJoinRankDaos() {
        return Result.success(ecologyPoolService.getEnableJoinRankDaos());
    }

    @ApiOperation(value = "查询已生态池的品牌", notes = "查询已生态池的品牌", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getEnableEcologyPools", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<EcologyPoolGetEnableJoinRankDaoPageVo>> getEnableEcologyPools() {
        return Result.success(ecologyPoolService.getEnableEcologyPools());
    }

    @ApiOperation(value = "优秀品牌池批量分配/扣除流水", notes = "优秀品牌池批量分配/扣除流水", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/excellentBrandBatchAllocatePoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @SneakyThrows
    public Result<Boolean> excellentBrandBatchAllocatePoints(@RequestBody @Valid EcologyPoolExcellentBrandBatchAllocatePointsRequest request) {
        ecologyPoolService.excellentBrandBatchAllocatePoints(request.getOperationType(), request.getRemark(), request.getDaoList());
        return Result.success();
    }


    @PostMapping(value = "/excellentBrandUpload", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "优秀品牌池批量分配上传品牌", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "uploadFile", value = "文件流对象", required = true, dataType = "__File")})
    @SysMemberLogin
    @SneakyThrows
    public Result<EcologyPoolExcellentBrandUploadAllVO> excellentBrandUpload(MultipartFile uploadFile) {
        return Result.success(ecologyPoolDaoRankService.excellentBrandUpload(uploadFile));
    }

    @SneakyThrows
    @ApiOperation(value = "优秀品牌池下载模板", notes = "优秀品牌池下载模板", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/excellentBrandDownloadTemplate", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public void excellentBrandDownloadTemplate(HttpServletResponse response) {
        List<UserCreatorInfoExcelVO> data = ecologyPoolService.excellentBrandDownloadTemplate();
//        List<UserCreatorInfoExcelVO> data = userCreatorInfoService.listUserCreatorByPool();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("优秀品牌模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserCreatorInfoExcelVO.class).sheet("品牌榜").doWrite(data);
    }

    @ApiOperation(value = "品牌榜查询", notes = "品牌榜查询", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoRankConfPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<EcologyPoolDaoRankConfVO> daoRankConfPage(@RequestBody EcologyPoolDaoRankConfRequest request) {
        return PageResult.success(ecologyPoolDaoRankConfService.daoRankConfPage(request.getPageNo(), request.getPageSize(), request.getRankName()), EcologyPoolDaoRankConfVO.class);
    }

    @ApiOperation(value = "品牌榜最后关闭时间", notes = "品牌榜最后关闭时间", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getRankConfLastTime", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Date> getRankConfLastTime(@RequestBody EcologyPoolRankConfGetLastTimeRequest request) {
        Date rankConfLastTime = ecologyPoolDaoRankConfService.getRankConfLastTime(request.getRankType());
        return Result.success(rankConfLastTime);
    }

    @ApiOperation(value = "新建品牌榜", notes = "新建品牌榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/createDaoRankConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> createDaoRankConf(@RequestBody @Valid EcologyPoolCreateDaoRankConfRequest request) {
        ecologyPoolDaoRankConfService.createDaoRankConf(request.getRankName(), request.getRankType(), request.getUserRatio(), request.getEcologyRatio(), request.getCashInRatio(), request.getContributionThreshold(), request.getStartTime(), request.getEndTime(), request.getActivityRule(), request.getTopImage(), request.getIncomeList());
        return Result.success();
    }

    @ApiOperation(value = "删除品牌榜", notes = "删除品牌榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/deleteDaoRankConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> deleteDaoRankConf(@RequestBody @Valid IdRequest request) {
        ecologyPoolDaoRankConfService.deleteDaoRankConf(request.getId());
        return Result.success();
    }

    @ApiOperation(value = "编辑品牌榜", notes = "编辑品牌榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateDaoRankConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> updateDaoRankConf(@RequestBody @Valid EcologyPoolUpdateDaoRankConfRequest request) {
        ecologyPoolDaoRankConfService.updateDaoRankConf(request.getId(), request.getActivityRule(), request.getTopImage());
        return Result.success();
    }

    @ApiOperation(value = "品牌榜上下架", notes = "品牌榜上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateDaoRankConfStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<Boolean> updateDaoRankConfStatus(@RequestBody @Valid EcologyPoolUpdateDaoRankConfStatusRequest request) {
        ecologyPoolDaoRankConfService.updateDaoRankConfStatus(request.getId(), request.getStatus());
        return Result.success(request.getStatus());
    }

    @ApiOperation(value = "品牌榜详情", notes = "品牌榜详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoRankConfDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<EcologyPoolDaoRankConfDetailVO> daoRankConfDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolDaoRankConfService.daoRankConfDetail(request.getId()));
    }

    @SneakyThrows
    @PostMapping(value = "/daoRankExcel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "品牌榜详情导出excel", notes = "品牌榜详情导出excel", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public void daoRankExcel(@RequestBody @Valid IdRequest request, HttpServletResponse response) {
        List<EcologyPoolDaoRankExcelVO> data = ecologyPoolDaoRankService.daoRankExcel(request.getId());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("品牌榜", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), EcologyPoolDaoRankExcelVO.class).sheet("品牌榜").doWrite(data);
    }

    @ApiOperation(value = "品牌榜榜单-数据详情", notes = "品牌榜榜单-数据详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoRankPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<EcologyPoolDaoRankPageVO> daoRankPage(@RequestBody @Valid IdPageRequest request) {
        return PageResult.success(ecologyPoolDaoRankService.daoRankPage(request.getPageNo(), request.getPageSize(), request.getId()), EcologyPoolDaoRankPageVO.class);
    }

    @ApiOperation(value = "服务中心/社区网点排行榜分页-非玛特DAO", notes = "服务中心/社区网点排行榜分页-非玛特DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoCommunityServiceRankConfPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolDaoCommunityServiceRankConfVo> daoCommunityServiceRankConfPage(@RequestBody @Valid EcologyPoolDaoCommunityServiceRankConfRequest request) {
        return PageResult.success(ecologyPoolService.daoCommunityServiceRankConfPage(request.getPageNo(), request.getPageSize(), request.getRankName(), request.getDaoId(), request.getApprovalStatus()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜-配置详情-非玛特DAO", notes = "服务中心/社区网点排行榜-配置详情-非玛特DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoCommunityServiceRankConfDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyPoolDaoCommunityServiceRankConfDetailVo> daoCommunityServiceRankConfDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolService.daoCommunityServiceRankConfDetail(request.getId()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜-数据详情-非玛特DAO", notes = "服务中心/社区网点排行榜-数据详情-非玛特DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoCommunityServiceRankPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyPoolDaoCommunityServiceRankVO> daoCommunityServiceRankPage(@RequestBody @Valid IdPageRequest request) {
        return PageResult.success(ecologyPoolService.daoCommunityServiceRankPage(request.getPageNo(), request.getPageSize(), request.getId()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜审核-非玛特DAO", notes = "服务中心/社区网点排行榜审核-非玛特DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoCommunityServiceRankConfApproval", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<Boolean> daoCommunityServiceRankConfApproval(@RequestBody @Valid EcologyPoolDaoCommunityServiceRankConfApprovalRequest request) {
        return Result.success(ecologyPoolService.daoCommunityServiceRankConfApproval(request.getId(), request.getApprovalStatus(), request.getApprovalMessage()));
    }

    @ApiOperation(value = "服务中心/社区网点排行榜上下架-非玛特DAO", notes = "服务中心/社区网点排行榜上下架-非玛特DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoUpdateCommunityServiceRankConfStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<Boolean> daoUpdateCommunityServiceRankConfStatus(@RequestBody @Valid EcologyPoolDaoUpdateCommunityServiceRankConfStatusRequest request) {
        return Result.success(ecologyPoolService.updateCommunityServiceRankConfStatus(request.getId(), request.getStatus()));
    }

    @ApiOperation(value = "获取熔炼藏品", notes = "获取熔炼藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hatchCollectionList", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<EcologyPoolHatchCollectionVo>> hatchCollectionList(@RequestBody @Valid EcologyPoolHatchCollectionRpcRequest request) {
        return Result.success(ecologyPoolService.hatchCollectionList(request.getCollectionName(), request.getDaoId()));
    }

    @ApiOperation(value = "根据品牌获取生态池积分名称", notes = "根据品牌获取生态池积分名称", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPoolNameByDaoId", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<EcologyPoolGetPoolNameByDaoIdVo> getPoolNameByDaoId(@RequestBody @Valid EcologyPoolGetPoolNameByDaoIdRequest request) {
        return Result.success(ecologyPoolService.getPoolNameByDaoId(request.getDaoId()));
    }

}
